<template>
	<view class="container">
		<nav-bar-sub></nav-bar-sub>

		<view class="u-m-t-20">
			<u-cell-group>
				<u-cell-item title="用户头像" :value="userInfoObject.name" @click="avatarChoose">
					<u-avatar slot="right-icon" :src="picSrc" size="100"></u-avatar>
				</u-cell-item>
				<u-cell-item title="姓名" :value="userInfoObject.realName" @click="navToEdit('name')"></u-cell-item>
				<u-cell-item title="地址" :value="userAddress" @click="navToEdit('address')"></u-cell-item>
				<u-cell-item title="手机号" :value="userPhone" @click="navToEdit('phone')"></u-cell-item>
				<u-cell-item title="邮箱" :value="userEmail" @click="navToEdit('email')"></u-cell-item>
				<u-cell-item title="出生日期" :value="birthday" @click="navToEdit('birthday')"></u-cell-item>
			</u-cell-group>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					name: '',
					intro: '',
					sex: ''
				},
				date: "请填写",
				userInfoObject: {},

				itemDisabled: true,
				noeditable: true,
				value: '请填写',
				sex: [{
					id: 1,
					name: '男'
				}, {
					id: 2,
					name: '女'
				}],
				saveOrEditValue: true,
				realNameM: '',
				addressM: '',
				phoneM: '',
				emailM: '',
				userBirM: '',

				userInfoUpdate: {
					realName: '',
					address: '',
					phone: '',
					email: '',
					userBir: ''
				},

				// date:'2000-01-01',
				index: 0,
				avater: '',
				description: '',
				url: '',
				nickName: '',
				mobile: '',
				headimg: '',
			}

		},
		methods: {
			// 页面跳转 传递对象 传对象
			navToEdit(type) {
				var userInfo = encodeURIComponent(JSON.stringify(this.userInfoObject))
				this.$u.route({
					url: 'pages/mine/infoEdit',
					params: {
						type,
						userInfo
					}
				})
			},
			
			avatarChoose() {
				var that = this
				this.$api.uploadImg('portrait')
					.then(res=>{
						this.$api.userInfo()
						.then(res => {
							console.log('', res)
							this.$store.state.userInfo = res.values
						})
					})
			},
			getUserInfo(e) {
				if (e.detail.iv) {
					console.log(e.detail.iv) //传后台解密换取用户信息
					uni.showToast({
						title: '已授权',
						icon: 'none',
						duration: 2000
					})
				}

			},
			getphonenumber(e) {
				if (e.detail.iv) {
					console.log(e.detail.iv) //传后台解密换取手机号
					uni.showToast({
						title: '已授权',
						icon: 'none',
						duration: 2000
					})
				}
			},
			editInfo() {
				this.saveOrEditValue = !this.saveOrEditValue
				this.itemDisabled = !this.itemDisabled
			},
			savaInfo() {
				var that = this
				for (let item in this.userInfoUpdate) { //k为键
					if (this.userInfoUpdate[item] == null || this.userInfoUpdate[item] == '') {
						delete this.userInfoUpdate.item
					}
				}
				// this.saveOrEditValue = !this.saveOrEditValue
				// this.itemDisabled = !this.itemDisabled
				this.$api.saveInfo(this.userInfoUpdate)
					.then(res => {
						console.log(res);
						if (res.status == 1) {
							// 修改失败
							this.$refs.uToast.show({
								title: res.message,
								type: 'default',
							})
						} else {
							// 修改成功
							this.$refs.uToast.show({
								title: res.message,
								type: 'success',
							})
							that.saveOrEditValue = !that.saveOrEditValue
							that.itemDisabled = !that.itemDisabled
						}
					})

			},
			isPoneAvailable(poneInput) {
				var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
				if (!myreg.test(poneInput)) {
					return false;
				} else {
					return true;
				}
			},
			async updata(datas) {
				//传后台

			},
			imgUpload(file) {
				let that = this;
				uni.uploadFile({
					header: {
						Authorization: uni.getStorageSync('token')
					},
					url: '/api/upload/image', //需传后台图片上传接口
					filePath: file[0],
					name: 'file',
					formData: {
						type: 'user_headimg'
					},
					success: function(res) {
						var data = JSON.parse(res.data);
						data = data.data;
						that.avater = that.url + data.img;

						that.headimg = that.url + data.img;
					},
					fail: function(error) {
						console.log(error);
					}
				});
			},

		},
		computed: {
			// userInfoUpdate() {
			// 	return {
			// 		realName: this.realNameM,
			// 		address: this.addressM,
			// 		phone: this.phoneM,
			// 		email: this.emailM,
			// 		userBir: this.userBirM
			// 	}
			// },
			picSrc: function() {
				return `${this.$store.state.base_url}/system/loadImg?imgPath=${encodeURI(this.$store.state.userInfo.photo)}&accessToken=${this.$store.state.access_token}`
			},
			userSex() {
				if (this.userInfoObject.sex == 1) {
					return '男'
				} else {
					return '女'
				}
			},
			birthday() {
				if (this.userInfoObject.birthdate == null) {
					return '未填写'
				} else {
					return this.userInfoObject.birthdate
				}
			},
			userAddress() {
				if (this.userInfoObject.address) {
					return this.userInfoObject.address
				} else {
					return '未填写'
				}
			},
			userEmail() {
				if (this.userInfoObject.email) {
					return this.userInfoObject.email
				} else {
					return '未填写'
				}
			},
			userPhone() {
				if (this.userInfoObject.phone) {
					return this.userInfoObject.phone
				} else {
					return '未填写'
				}
			}
		},
		onShow() {
			this.$api.userInfo()
				.then(res => {
					console.log(res);
					this.userInfoObject = res.values
					if (res.values.realName) {
						this.realNameM = res.values.realName
					}
					if (res.values.address) {
						this.addressM = res.values.address
					}
					if (res.values.phone) {
						this.phoneM = res.values.phone
					}
					if (res.values.email) {
						this.emailM = res.values.email
					}
					if (res.values.birthdate) {
						this.userBirM = res.values.birthdate
					}
				})
		}

	}
</script>

<style lang="scss">
	.container {
		display: block;
	}

	.ui-all {
		padding: 20rpx 40rpx;

		.avatar {
			display: flex;
			justify-content: space-between;
			align-items: center;
			border-bottom: solid 1px #f2f2f2;

			.avatarTips {
				margin-left: 80rpx;
				color: #999;
			}
		}

		.ui-list {
			width: 100%;
			text-align: left;
			padding: 20rpx 0;
			border-bottom: solid 1px #f2f2f2;
			position: relative;

			text {
				color: #4a4a4a;
				font-size: 28rpx;
				display: inline-block;
				vertical-align: middle;
				min-width: 150rpx;
			}

			input {
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
			}

			button {
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				background: none;
				margin: 0;
				padding: 0;

				&::after {
					display: none;
				}
			}

			picker {
				width: 90%;
				color: #030303;
				font-size: 30rpx;
				display: inline-block;
				vertical-align: middle;
				position: absolute;
				top: 30rpx;
				left: 150rpx;
			}

			textarea {
				color: #030303;
				font-size: 30rpx;
				vertical-align: middle;
				height: 150rpx;
				width: 100%;
				margin-top: 50rpx;
			}

			.place {
				color: #999999;
				font-size: 28rpx;
			}
		}

		.right:after {
			content: ' ';
			width: 20rpx;
			height: 20rpx;
			border-top: solid 1px #030303;
			border-right: solid 1px #030303;
			transform: rotate(45deg);
			-ms-transform: rotate(45deg);
			/* IE 9 */
			-moz-transform: rotate(45deg);
			/* Firefox */
			-webkit-transform: rotate(45deg);
			/* Safari 和 Chrome */
			-o-transform: rotate(45deg);
			position: absolute;
			top: 40rpx;
			right: 0;
		}

		.save {
			background: #030303;
			border: none;
			color: #ffffff;
			margin-top: 40rpx;
			font-size: 28rpx;
		}
	}
</style>
